package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import lombok.AllArgsConstructor;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.modules.hc.entity.HcCompanyDetail;
import org.jeecg.modules.hc.entity.HcCompanyFoodDisplay;
import org.jeecg.modules.hc.entity.HcCompanyMerchantInfo;
import org.jeecg.modules.hc.entity.HcMerchant;
import org.jeecg.modules.hc.mapper.HcCompanyDetailMapper;
import org.jeecg.modules.hc.mapper.HcCompanyFoodDisplayMapper;
import org.jeecg.modules.hc.mapper.HcCompanyMerchantInfoMapper;
import org.jeecg.modules.hc.mapper.HcMerchantMapper;
import org.jeecg.modules.hc.service.IHcCompanyDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 企业详情表
 * @Author: jeecg-boot
 * @Date:   2023-07-04
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyDetailServiceImpl extends ServiceImpl<HcCompanyDetailMapper, HcCompanyDetail> implements IHcCompanyDetailService {
    private HcMerchantMapper hcMerchantMapper;
    private HcCompanyDetailMapper hcCompanyDetailMapper;
    private HcCompanyFoodDisplayMapper hcCompanyFoodDisplayMapper;
    private HcCompanyMerchantInfoMapper hcCompanyMerchantInfoMapper;

    @Override
    public void saveNew(HcCompanyDetail hcCompanyDetail) {
        String storesNames = getStoresNames(hcCompanyDetail.getListingStoreIds());
        hcCompanyDetail.setListingStore(storesNames);
        hcCompanyDetailMapper.insert(hcCompanyDetail);
    }

    @Override
    public void updateNew(HcCompanyDetail hcCompanyDetail) {
        String listingStoreIds = hcCompanyDetail.getListingStoreIds();
        String storesNames = getStoresNames(listingStoreIds);
        hcCompanyDetail.setListingStore(storesNames);
        List<String> strings = Arrays.asList(listingStoreIds.split(","));
        strings.forEach(s -> {
            LambdaQueryWrapper<HcCompanyMerchantInfo> hcCompanyMerchantInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            hcCompanyMerchantInfoLambdaQueryWrapper.eq(HcCompanyMerchantInfo::getMerchantId,s)
                    .eq(HcCompanyMerchantInfo::getCompanyId,hcCompanyDetail.getCompanyId()).last("limit 1");
            HcCompanyMerchantInfo hcCompanyMerchantInfo = hcCompanyMerchantInfoMapper.selectOne(hcCompanyMerchantInfoLambdaQueryWrapper);
            if (null==hcCompanyMerchantInfo){
                //添加企业和商户关系
                HcCompanyMerchantInfo Info = new HcCompanyMerchantInfo();
                Info.setCompanyId(hcCompanyDetail.getCompanyId());
                Info.setMerchantId(s);
                hcCompanyMerchantInfoMapper.insert(Info);
            }
        });
        hcCompanyDetailMapper.updateById(hcCompanyDetail);
    }

    @Override
    @Transactional
    public void createTranscript(String id,String date) {
        HcCompanyDetail hcCompanyDetail = hcCompanyDetailMapper.selectById(id);

//        String menuDisplayTime = hcCompanyDetail.getMenuDisplayTime();
        String menuDisplayTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 将字符串转换为 LocalDate 对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.parse(menuDisplayTime, formatter);
        // 将菜单显示时间增加一天
        LocalDate nextDay = currentDate.plusDays(1);
        // 将增加一天后的日期转换为字符串
        String updatedMenuDisplayTime = nextDay.format(formatter);
//        hcCompanyDetail.setMenuDisplayTime(updatedMenuDisplayTime);
        hcCompanyDetail.setMenuDisplayTime(date);
        String detailsId = UuidUtils.getUUID();
        hcCompanyDetail.setId(detailsId);
        hcCompanyDetail.setCreateTime(new Date());
        hcCompanyDetailMapper.insert(hcCompanyDetail);

        //将原来的菜品副本和新副本绑定
        List<HcCompanyFoodDisplay> hcCompanyFoodDisplays = hcCompanyFoodDisplayMapper
                .selectList(new LambdaQueryWrapper<HcCompanyFoodDisplay>().eq(HcCompanyFoodDisplay::getCompanyDetailId, id));
        for (HcCompanyFoodDisplay hcCompanyFoodDisplay : hcCompanyFoodDisplays) {
            String displayId = UuidUtils.getUUID();
            hcCompanyFoodDisplay.setId(displayId);
            hcCompanyFoodDisplay.setCompanyDetailId(detailsId);
            hcCompanyFoodDisplayMapper.insert(hcCompanyFoodDisplay);
        }
        //更换企业和商户的关系
        int i = hcCompanyMerchantInfoMapper.delete(new LambdaQueryWrapper<HcCompanyMerchantInfo>().eq(HcCompanyMerchantInfo::getCompanyId, hcCompanyDetail.getCompanyId()));
        if (i>0){
            List<String> merchantIds = Arrays.asList(hcCompanyDetail.getListingStoreIds().split(","));
            if (CollectionUtils.isNotEmpty(merchantIds)){
                merchantIds.forEach(m -> {
                    hcCompanyMerchantInfoMapper.insert(new HcCompanyMerchantInfo().setCompanyId(hcCompanyDetail.getCompanyId()).setMerchantId(m));
                });
            }
        }

    }

    @Override
    @Transactional
    public void deleteMerchant(String merchantId,String detailId) {
        HcCompanyDetail hcCompanyDetail = hcCompanyDetailMapper.selectOne(new LambdaQueryWrapper<HcCompanyDetail>().eq(HcCompanyDetail::getId, detailId));

        //删除关系
        LambdaQueryWrapper<HcCompanyMerchantInfo> merchantInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        merchantInfoLambdaQueryWrapper.eq(HcCompanyMerchantInfo::getCompanyId,hcCompanyDetail.getCompanyId())
                .eq(HcCompanyMerchantInfo::getMerchantId,merchantId);
        hcCompanyMerchantInfoMapper.delete(merchantInfoLambdaQueryWrapper);

        String listingStoreIds = hcCompanyDetail.getListingStoreIds();

        String ids = "";
        String storesNames = "";
        if (listingStoreIds.contains(",")){
            List<String> idList = Arrays.asList(listingStoreIds.split(","));
            if (idList.size()>1){
                List<String> idNew = new ArrayList<>();
                for (String s : idList) {
                    if (!s.equals(merchantId)){
                        idNew.add(s);
                    }
                }
                ids = Joiner.on(",").join(idNew);
                storesNames = getStoresNames(ids);
            }else {
                ids = idList.get(0);
                HcMerchant hcMerchant = hcMerchantMapper.selectOne(new LambdaQueryWrapper<HcMerchant>().eq(HcMerchant::getId, ids));
                storesNames = hcMerchant.getStoreName();
            }
        }
        hcCompanyDetail.setListingStoreIds(ids);
        hcCompanyDetail.setListingStore(storesNames);
        hcCompanyDetailMapper.updateById(hcCompanyDetail);
        //删除展示关系
        LambdaQueryWrapper<HcCompanyFoodDisplay> displayLambdaQueryWrapper = new LambdaQueryWrapper<>();
        displayLambdaQueryWrapper.eq(HcCompanyFoodDisplay::getCompanyId,hcCompanyDetail.getCompanyId())
                .eq(HcCompanyFoodDisplay::getMerchantId,merchantId);
        List<HcCompanyFoodDisplay> hcCompanyFoodDisplays = hcCompanyFoodDisplayMapper.selectList(displayLambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(hcCompanyFoodDisplays)){
            hcCompanyFoodDisplayMapper.delete(displayLambdaQueryWrapper);
        }
    }

    private String getStoresNames(String listingStoreIds){
        List<String> ids = Arrays.asList(listingStoreIds.split(","));
        List<String> nameList = new ArrayList<>();
        for (String id : ids) {
            LambdaQueryWrapper<HcMerchant> merchantLambdaQueryWrapper = new LambdaQueryWrapper<>();
            merchantLambdaQueryWrapper.eq(HcMerchant::getId,id);
            HcMerchant hcMerchant = hcMerchantMapper.selectOne(merchantLambdaQueryWrapper);
            nameList.add(hcMerchant.getStoreName());
        }
        return Joiner.on(",").join(nameList);
    }
}
